/**
 * 
 */
package com.desksoft.ecommerce.handler.filter;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.JsonParser;

/***************************************
 * 版权所有：杭州迪科软件 保留所有权利
 * 创建日期: 
 * 创建作者：yejw
 * 文件名称：
 * 版本：
 * 功能：字符串编码的Filter 为了解决中文乱码的问题; 需要配合web.xml文件配置才能完成功能
 * 最后修改时间：
 * 修改记录：
 *****************************************/
public class EncodingFilter implements Filter {
    // 默认GBK
    protected String encoding = "UTF-8";

    protected boolean ignore = true;
    
    protected boolean setContentType = true;
    
    protected static Set<String> EXT_SET = new HashSet<String>();
    
    static {
        EXT_SET.add("htm");
        EXT_SET.add("html");
        EXT_SET.add("shtml");
        EXT_SET.add("jsp");
        EXT_SET.add("do");
        EXT_SET.add("vm");
        EXT_SET.add("jspa");
    }

    /*
     * 过滤器环境初始化
     */
    public void init(FilterConfig filterConfig) throws ServletException {
        String paramValue = filterConfig.getInitParameter("encoding");

        if (paramValue != null) {
            this.encoding = paramValue;
        }

        String value = filterConfig.getInitParameter("ignore");

        if (value == null) {
            this.ignore = true;
        } else if (value.equalsIgnoreCase("true")) {
            this.ignore = true;
        } else if (value.equalsIgnoreCase("yes")) {
            this.ignore = true;
        } else {
            this.ignore = false;
        }
        
        String contentType = filterConfig.getInitParameter("response-head");
        if ("false".equals(contentType)) {
            setContentType = false;
        }
    }

    /*
     * 过滤器工作，解决中文乱码的问题；
     */
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        if (ignore || (request.getCharacterEncoding() == null)) {
            String encoding = selectEncoding(request);

            if (encoding != null) {
                request.setCharacterEncoding(encoding);
            }
        }
        
        if (setContentType) {
            // 设置响应头信息
            HttpServletRequest httpRequest = (HttpServletRequest) request;
            String uri = StringUtils.lowerCase(httpRequest.getRequestURI());
            String ext = FilenameUtils.getExtension(uri);
            if (EXT_SET.contains(ext)) {
                response.setContentType("text/html;charset="+this.encoding);
            } else {
                response.setCharacterEncoding(this.encoding);
            }
        }
        
        
        chain.doFilter(request, response);
    }

    /*
     * (non-Javadoc)
     * 
     * @see javax.servlet.Filter#destroy()
     */
    public void destroy() {
    }

    protected String selectEncoding(ServletRequest request) {
        return (this.encoding);
    }
}
